<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属流程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processTriggerId">
              <!-- <a-input v-model="model.processTriggerId" placeholder="请输入所属流程id"></a-input> -->
              <a-select placeholder="请选择" v-model="model.processTriggerId" show-search optionFilterProp="children">
                <a-select-option v-for="mode in processList" :key="mode.id" :value="mode.id">
                  {{ mode.processName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="事件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eventType">
              <a-select
                @change="changeEventType"
                placeholder="请选择"
                v-model="model.eventType"
                show-search
                optionFilterProp="children"
              >
                <a-select-option value="TOUCH_PROCESS"> 点击 </a-select-option>
                <a-select-option value="PACKAGE_GRID_ERROR"> 工程量值判断 </a-select-option>
                <a-select-option value="RECEIVE_PACKAGE"> 接收指定包 </a-select-option>
              </a-select>
              <!-- <a-input
                v-model="model.eventType"
                placeholder="请输入点击 touch_process,工程量值判断 package_grid_error ,接收指定包 receive_package"
              ></a-input> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.eventType == 'TOUCH_PROCESS'">
            <a-form-model-item label="点击的流程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processId">
              <a-select placeholder="请选择" v-model="model.processId" show-search optionFilterProp="children">
                <a-select-option v-for="mode in processList" :key="mode.id" :value="mode.id">
                  {{ mode.processName }}
                </a-select-option>
              </a-select>
              <!-- <a-input v-model="model.processId" placeholder="请输入点击的流程id touch_process事件"></a-input> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.eventType == 'PACKAGE_GRID_ERROR'">
            <a-form-model-item label="异常工程量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageGridId">
              <!-- <a-input v-model="model.packageGridId" placeholder="请输入异常工程量id package_grid_error "></a-input> -->
              <a-select placeholder="请选择" v-model="model.packageGridId" show-search optionFilterProp="children">
                <a-select-option v-for="mode in packageGridList" :key="mode.id" :value="mode.id">
                  {{ mode.gridName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.eventType == 'PACKAGE_GRID_ERROR'">
            <a-form-model-item
              label="异常工程量值"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="packageGridTriggerValue"
            >
              <!-- <a-select
                placeholder="请选择"
                v-model="model.packageGridTriggerValue"
                show-search
                optionFilterProp="children"
              >
                <a-select-option v-for="mode in packageGridValueList" :key="mode.id" :value="mode.packageGridValue">
                  {{ mode.packageGridValue }}
                </a-select-option>
              </a-select> -->
              <a-input v-model="model.packageGridTriggerValue" placeholder="请输入异常工程量值"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.eventType == 'RECEIVE_PACKAGE'">
            <a-form-model-item label="接收包" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageId">
              <a-select placeholder="请选择" show-search optionFilterProp="children" v-model="model.packageId">
                <a-select-option v-for="mode in PackageList" :key="mode.id" :value="mode.id" :label="mode.name">
                  {{ mode.packageName }}
                </a-select-option>
              </a-select>
              <!-- <a-input v-model="model.packageId" placeholder="请输入接收包id receive_package"></a-input> -->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'NmcProcessConditionForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/process/nmcProcessCondition/add',
        edit: '/process/nmcProcessCondition/edit',
        queryById: '/process/nmcProcessCondition/queryById',
        processList: '/process/nmcProcessTrigger/list',
        packageGridList: '/grid/nmcPackageGrid/list',
        packageGridValueList: '/grid/nmcPackageGridValue/list',
        PackageList: '/grid/nmcPackageInfo/list',
      },
      PackageList: [],
      processList: [],
      packageGridList: [],
      packageGridValueList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getLists()
  },
  methods: {
    changeEventType() {},
    handleChange(value) {
      console.log(value)
      // this.value = value
      fetch(value, (data) => (this.PackageList = data))
    },
    handleSearch(value) {
      fetch(value, (data) => (this.PackageList = data))
    },
    getLists() {
      getAction(this.url.processList, { pageSize: -1 }).then((res) => {
        if (res.success) {
          this.processList = res.result.records
        } else {
          this.$message.warning(res.message)
        }
      })
      getAction(this.url.packageGridList, { pageSize: -1 }).then((res) => {
        if (res.success) {
          this.packageGridList = res.result.records
        } else {
          this.$message.warning(res.message)
        }
      })
      getAction(this.url.packageGridValueList, { pageSize: -1 }).then((res) => {
        if (res.success) {
          this.packageGridValueList = res.result.records
          console.log(
            res.result.records.find((item) => item.id == '0022'),
            '159'
          )
        } else {
          this.$message.warning(res.message)
        }
      })
      getAction(this.url.PackageList, { pageSize: -1 }).then((res) => {
        if (res.success) {
          this.PackageList = res.result.records
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.model.eventType == 'TOUCH_PROCESS') {
            this.model.packageId = null //接受包
            this.model.packageId_dictText = null
            this.model.packageGridTriggerValue = null //异常值
            this.model.packageGridId = null //异常工程量
            this.model.packageGridId_dictText = null
          }
          if (this.model.eventType == 'PACKAGE_GRID_ERROR') {
            this.model.processId = null //流程
            this.model.processId_dictText = null
            this.model.packageId = null //接受包
            this.model.packageId_dictText = null
          }
          if (this.model.eventType == 'RECEIVE_PACKAGE') {
            this.model.processId = null //流程
            this.model.processId_dictText = null
            this.model.packageGridTriggerValue = null //异常值
            this.model.packageGridId = null //异常工程量
            this.model.packageGridId_dictText = null
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
